package com.sbio.udiapp.util;

import com.sbio.udiapp.di.ActivityManager;
import com.sbio.udiapp.net.api.ApiService;
import com.sbio.udiapp.net.bean.AjaxResult;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FileUtil {

    public static void uploadLog(ApiService apiService, File file) {
        String deviceNo = DefaultSharedPreferencesUtil.getDeviceNo(ActivityManager.getCurrentActivity());
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);

// 创建文件的 MultipartBody.Part
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
        Call<AjaxResult> call = apiService.uploadLog(filePart,deviceNo);
        call.enqueue(new Callback<AjaxResult>() {
            @Override
            public void onResponse(Call<AjaxResult> call, Response<AjaxResult> response) {
                // 处理请求成功的响应
                if (response.isSuccessful()) {
                    // 文件上传成功
                    AjaxResult ajaxResult = response.body();
                    if (ajaxResult.isSuccess()) {
                        ToastUtil.show("文件上传成功");
                    } else {
                        ToastUtil.show("文件上传失败" + ajaxResult.getMsg());
                    }
                } else {
                    // 文件上传失败
                    ToastUtil.show("文件上传失败2");
                }
            }

            @Override
            public void onFailure(Call<AjaxResult> call, Throwable t) {

                // 处理请求失败
                ToastUtil.show("文件上传失败,请求异常");
            }
        });
    }
}
